﻿using System;
using System.Text;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace AURP.SP.ListEventReceiver.AURPEventReceiver
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class AURPEventReceiver : SPItemEventReceiver
    {
       /// <summary>
       /// An item is being added.
       /// </summary>
       public override void ItemAdding(SPItemEventProperties properties)
       {
           base.ItemAdding(properties);
           SPListItem item = GetLiistItem(properties);           
           string counter = String.Empty;
           if (item != null)
           {
               counter = GetCounter(item);
               properties.AfterProperties["Title"] = counter ;
               if(Convert.ToString(item["Prefix"]).Length>0)
                   item["CurrentCount"] = counter.Replace(Convert.ToString(item["Prefix"]), "");
               else
                   item["CurrentCount"] = counter;
               item.Update();
           }
           else
           {

           }

       }

       /// <summary>
       /// An item is being updated.
       /// </summary>
       public override void ItemUpdating(SPItemEventProperties properties)
       {
           base.ItemUpdating(properties);
       }

       /// <summary>
       /// An item is being deleted.
       /// </summary>
       public override void ItemDeleting(SPItemEventProperties properties)
       {
           base.ItemDeleting(properties);
       }

       /// <summary>
       /// An item was added.
       /// </summary>
       public override void ItemAdded(SPItemEventProperties properties)
       {
           base.ItemAdded(properties);
       }

       /// <summary>
       /// An item was updated.
       /// </summary>
       public override void ItemUpdated(SPItemEventProperties properties)
       {
           base.ItemUpdated(properties);
       }

       /// <summary>
       /// An item was deleted.
       /// </summary>
       public override void ItemDeleted(SPItemEventProperties properties)
       {
           base.ItemDeleted(properties);
       }

       public static SPListItem GetLiistItem(SPItemEventProperties properties)
       {
           SPListItem listItem = null;
           SPList list = null;
           if(properties.Web.IsRootWeb)
               list = properties.Web.Lists.TryGetList("Auto Increment Counter");
           else
               list = properties.Web.ParentWeb.Lists.TryGetList("Auto Increment Counter");

           if (list != null)
           {
               SPQuery query = new SPQuery();
               query.Query = string.Concat("    <Where> <Eq> ",
                                                    "<FieldRef Name='ListGUID' /> ",
                                                    "<Value Type='Text'>"+properties.ListId+"</Value> ",
                                                    "</Eq> </Where>");
               query.ViewFields = string.Concat(
                    " <FieldRef Name='ListGUID' /> ",
                    "<FieldRef Name='LinkToList' /> ",
                    "<FieldRef Name='Prefix' /> ",
                    "<FieldRef Name='DefaultCount' /> ",
                    "<FieldRef Name='CurrentCount' /> ",
                    "<FieldRef Name='Remarks' /> ");
               //query.ViewFieldsOnly = true;
               SPListItemCollection items = list.GetItems(query);
               foreach (SPListItem item in items)               
                   listItem = item;                   
           }
           return listItem;
       }

       public static String GetCounter(SPListItem item)
       {
           string counter = String.Empty;
           int intValue ;
           string formater = String.Empty;
           string prefix = Convert.ToString(item["Prefix"]);                      
           for (int i = 0; i < Convert.ToString(item["DefaultCount"]).Length; i++)
               formater = String.Concat("0", formater);

           if (String.IsNullOrEmpty(Convert.ToString(item["CurrentCount"])))           
               intValue = Convert.ToInt32(item["DefaultCount"]);
           else
               intValue = Convert.ToInt32(item["CurrentCount"]);
           intValue = intValue + 1;
           counter = string.Format("{0}{1}", prefix, intValue.ToString(formater));           

           return counter;
       }


    }
}
